
#include "main.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

int texture_load(const char* filename)
{
	SDL_Surface* surf = IMG_Load(filename);
	if (!surf)
	{
		printf("texture_load failed: %s\n", IMG_GetError());
		return -1;
	}

	// if we don't have GL_ARB_texture_rectangle, we have to resize it

	int fmt = surf->format->Amask ? GL_RGBA : GL_RGB;
	
	GLuint id;
	glGenTextures(1, &id);
	glBindTexture(GL_TEXTURE_2D, id);
	gluBuild2DMipmaps(GL_TEXTURE_2D, fmt,
		surf->w, surf->h, fmt, GL_UNSIGNED_BYTE, surf->pixels);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	SDL_FreeSurface(surf);

	return id;
}
